{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cbf9e9d4",
   "metadata": {},
   "source": [
    "# 人脸搜索 & 人脸库管理API组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc7b90e2",
   "metadata": {},
   "source": [
    "## 人脸库管理API组  \n",
    "### 1.[创建人脸库API](https://console.faceplusplus.com.cn/documents/4888391)  \n",
    "* 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。  \n",
    "---\n",
    "* 返回值说明：  \n",
    "  * ace_added——本次操作成功加入 FaceSet的face_token 数量  \n",
    "  * face_count——操作结束后 FaceSet 中的 face_token 总数量  \n",
    "  * outer_id——用户自定义的 FaceSet 标识，如果未定义则返回值为空  \n",
    "  * **faceset_token——FaceSet 的标识**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23619fa2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'time_used': 539,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647416359,2e3b6b1d-bdfb-46ee-99b4-b2dbdf5bc5e4',\n",
       " 'outer_id': 'newfaceset',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 3.通行证\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"  \n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"face_token\":\"4f8de912794612e8618940c8e7e84ddf\",\n",
    "    \"outer_id\":\"newfaceset\",\n",
    "    \"face_tokens\":\"65e949355ac17072fd91a945f8af637b\"\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1ca35e0",
   "metadata": {},
   "source": [
    "###  2.[添加人脸API](https://console.faceplusplus.com.cn/documents/4888389)  \n",
    "* 为一个已经创建的 FaceSet 添加人脸标识 face_token   \n",
    "* face_tokens:需要添加的人脸标识 face_token 组成的字符串，可以是一个或者多个，用逗号分隔。最多不超过5个face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e1d01c7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 由于需要添加face_token 所以最好先写个人脸检测的函数 方便后续添加删除修改\n",
    "\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "\n",
    "\n",
    "def face_detect(API_key,API_sercret,image_url,landmark=None,attributes=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "628d9313",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647417291,31afa0e0-575b-46d9-85e0-5d86d9e6bef7',\n",
       " 'time_used': 243,\n",
       " 'faces': [{'face_token': 'befd5bf20ea0047c2c565fae0dd417ee',\n",
       "   'face_rectangle': {'top': 163, 'left': 143, 'width': 198, 'height': 198}}],\n",
       " 'image_id': '96dmUXC2cBisy3yYgwMOug==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# face_token02\n",
    "face_detect(API_key,API_sercret,\"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3285531607,1923997306&fm=170&s=64F919D795364394E385A8F90300D063&w=449&h=488&img.PNG\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2c32fdae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647417728,0fd52bc9-f78a-4689-a4db-4e1cc8b921a2',\n",
       " 'time_used': 333,\n",
       " 'faces': [{'face_token': 'c77e925e6a8d6ef0f69051b9db79fb84',\n",
       "   'face_rectangle': {'top': 92, 'left': 60, 'width': 121, 'height': 121}}],\n",
       " 'image_id': '8mbV9EsJDJcMra26tJYc5w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# face_token03\n",
    "face_detect(API_key,API_sercret,\"https://p3.ssl.qhimgs1.com/sdr/400__/t013d401572f99b4479.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f5669ca9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647417820,7fd71495-97dd-4f72-831e-9d639ee1d1ff',\n",
       " 'time_used': 386,\n",
       " 'faces': [{'face_token': 'c49158756699cbba2c07ec0b664f4100',\n",
       "   'face_rectangle': {'top': 189, 'left': 210, 'width': 148, 'height': 148}}],\n",
       " 'image_id': 'tJGezwW9DcferWr3TN9GRw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# face_token04\n",
    "face_detect(API_key,API_sercret,\"http://img.67.com/upload/images/2021/06/25/c2hlbnppY2hhbzE2MjQ1ODE3NTg=_6.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "05fb9f8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647417942,45f01932-731c-4d93-8ab6-6586dca5d0bc',\n",
       " 'time_used': 230,\n",
       " 'faces': [{'face_token': '641d323f008514379357cced51ebd81a',\n",
       "   'face_rectangle': {'top': 193, 'left': 171, 'width': 275, 'height': 275}}],\n",
       " 'image_id': 'C24PbnMr5iMb0CUsbDj9Fg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# face_token05\n",
    "face_detect(API_key,API_sercret,\"http://5b0988e595225.cdn.sohucs.com/images/20190822/64c6f2cf891f4cd1ae81f00291811d4f.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cec04496",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'time_used': 677,\n",
       " 'face_count': 2,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647417354,f581faf8-9afc-404e-b9a7-eb889a22a3eb',\n",
       " 'outer_id': 'newfaceset',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "# 3.权限/通行证————所有 API Key 都可以调用本 API。\n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"newfaceset\",\n",
    "    \"face_tokens\":\"befd5bf20ea0047c2c565fae0dd417ee\"\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n",
    "\n",
    "# 注意返回值中face_count': 2 说明成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ae93574b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'time_used': 847,\n",
       " 'face_count': 5,\n",
       " 'face_added': 3,\n",
       " 'request_id': '1647417979,5079c737-dcc1-43c1-b889-d7fd150ba054',\n",
       " 'outer_id': 'newfaceset',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 添加多个face_tokens\n",
    "\n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"newfaceset\",\n",
    "    \"face_tokens\":\"c77e925e6a8d6ef0f69051b9db79fb84,c49158756699cbba2c07ec0b664f4100,641d323f008514379357cced51ebd81a\"\n",
    "}\n",
    " \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53e478cb",
   "metadata": {},
   "source": [
    "### 3.[获取人脸库信息 API ](https://console.faceplusplus.com.cn/documents/4888395) \n",
    "* 获取一个 FaceSet 的所有信息，包括此 FaceSet 的 faceset_token, outer_id, display_name 的信息，以及此 FaceSet 中存放的 face_token 数量与列表。  \n",
    "***\n",
    "* 返回值说明：  \n",
    " * faceset_token：该FaceSet的标识\n",
    " * outer_id：用户提供的FaceSet标识\n",
    " * display_name\t：人脸集合的名字\n",
    " * user_data：自定义用户信息\n",
    " * tags\t：自定义标签\n",
    " * face_count：FaceSet中的face_token总数量\n",
    " * face_tokens\t：face_token的数组   注：如果该 FaceSet 下没有 face_token，则返回空数组。\n",
    " * next：用于进行下一次请求。返回值表示排在此次返回的所有 face_token 之后的下一个 face_token 的序号。如果返回此字段，则说明未返回完此 FaceSet 下的所有 face_token。可以将此字段的返回值，在下一次调用时传入 start 字段中，获取接下来的 face_token。如果没有返回该字段，则说明已经返回此 FaceSet 下的所有 face_token。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cecc5352",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'tags': '',\n",
       " 'time_used': 101,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': ['65e949355ac17072fd91a945f8af637b',\n",
       "  'befd5bf20ea0047c2c565fae0dd417ee',\n",
       "  'c49158756699cbba2c07ec0b664f4100',\n",
       "  'c77e925e6a8d6ef0f69051b9db79fb84',\n",
       "  '641d323f008514379357cced51ebd81a'],\n",
       " 'face_count': 5,\n",
       " 'request_id': '1647420002,a1c0879a-d729-4ad0-87bb-201f9c005a88',\n",
       " 'outer_id': 'newfaceset'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "# 3.权限/通行证————所有 API Key 都可以调用本 API。\n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"newfaceset\"\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c9a56eaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'tags': '',\n",
       " 'time_used': 123,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': ['c49158756699cbba2c07ec0b664f4100',\n",
       "  'c77e925e6a8d6ef0f69051b9db79fb84',\n",
       "  '641d323f008514379357cced51ebd81a'],\n",
       " 'face_count': 5,\n",
       " 'request_id': '1647420427,25c6b3c9-e646-4163-aa4d-4f99803cded1',\n",
       " 'outer_id': 'newfaceset'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 请求参数start：一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是 [1,10000] 间的一个整数。\n",
    "#               通过传入数字 n，可以控制本 API 从第 n 个 face_token 开始返回。返回的 face_token 按照创建时间排序，每次返回 100 个 face_token。默认值为 1。\n",
    "\n",
    "\n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"newfaceset\",\n",
    "    \"start\":3\n",
    "}\n",
    "\n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2b07657",
   "metadata": {},
   "source": [
    "### 4.[删除人脸API](https://console.faceplusplus.com.cn/documents/4888399)  \n",
    "* 移除一个FaceSet中的某些或者全部face_token  \n",
    "* 需要移除的人脸标识字符串，可以是一个或者多个face_token组成，用逗号分隔"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fd29246e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 197,\n",
       " 'face_count': 4,\n",
       " 'request_id': '1647420598,3d6e8dc2-beee-4896-9d97-201a320397d8',\n",
       " 'outer_id': 'newfaceset',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \"  https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "# 3.权限/通行证————所有 API Key 都可以调用本 API。\n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"newfaceset\",\n",
    "    \"face_tokens\":\"c49158756699cbba2c07ec0b664f4100\"\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n",
    "\n",
    "# 观察结果 'face_count': 4  说明删除成功"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a170bb0",
   "metadata": {},
   "source": [
    "### 5.[更新人脸库信息 API  ](https://console.faceplusplus.com.cn/documents/4888401)\n",
    "* 更新一个人脸集合的属性  ，  可以更新outer_id、display_name、tags、user_data   \n",
    "---\n",
    "* 返回值：（基本上）  \n",
    " * request_id\n",
    " * faceset_token\n",
    " * outer_id\t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0b540523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'request_id': '1647422446,18ee2a55-3894-4795-bd66-7dfe7c6bbda6',\n",
       " 'time_used': 92,\n",
       " 'outer_id': 'firstfaceset'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "# 3.权限/通行证————所有 API Key 都可以调用本 API。\n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"newfaceset\",\n",
    "    \"new_outer_id\":\"firstfaceset\",\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "275aa213",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       " 'request_id': '1647422654,01a1e9e6-2fb2-49c3-bd6a-9e6c5c3a0c60',\n",
       " 'time_used': 72,\n",
       " 'outer_id': 'firstfaceset'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新display_name  news --> news_1\n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"firstfaceset\",\n",
    "    \"display_name\":\"news_1\",\n",
    "}\n",
    "\n",
    " \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc2a33ec",
   "metadata": {},
   "source": [
    "##### 再创建一个人脸库API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "016e0ea1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '7144c3dcf4cbf12f82647f948de5d1f5',\n",
       " 'time_used': 272,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647422451,3096571c-22c7-48d9-a121-5c3385db849f',\n",
       " 'outer_id': 'secondfaceset',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 3.通行证\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"  \n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"face_token\":\"4f8de912794612e8618940c8e7e84ddf\",\n",
    "    \"outer_id\":\"secondfaceset\",\n",
    "    \"display_name\":\"newface\",\n",
    "    \"tags\":\"star\"\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9766b34",
   "metadata": {},
   "source": [
    "### 6.[获取人脸库列表及信息 API ](https://console.faceplusplus.com.cn/documents/4888397) \n",
    "* 获取某一 API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。 \n",
    "---\n",
    "* 返回字段说明：  \n",
    "  * facesets：该 API Key 下的 FaceSet 信息（faceset_token、outer_id、display_name、tags）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e49f9257",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 89,\n",
       " 'facesets': [{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       "   'outer_id': 'firstfaceset',\n",
       "   'display_name': 'news_1',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '7144c3dcf4cbf12f82647f948de5d1f5',\n",
       "   'outer_id': 'secondfaceset',\n",
       "   'display_name': 'newface',\n",
       "   'tags': 'star'}],\n",
       " 'request_id': '1647422809,ed412bb9-be95-473f-9bc9-f0a41d97629d'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "# 3.权限/通行证————所有 API Key 都可以调用本 API。\n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n",
    "\n",
    "# 目前有2个人脸库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "184774ef",
   "metadata": {},
   "source": [
    "### 7.[删除人脸库API  ](https://console.faceplusplus.com.cn/documents/4888399)\n",
    "* 删除一个人脸集合\n",
    "---\n",
    "* 参数（可选）说明\n",
    " * check_empty\t：删除时是否检查FaceSet中是否存在face_token，默认值为1。0：不检查  ； 1：检查（如果设置为1，当FaceSet中存在face_token则不能删除）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "08f4f3d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '7144c3dcf4cbf12f82647f948de5d1f5',\n",
       " 'request_id': '1647423162,43b4ba44-0286-44fd-9817-852aecb357fa',\n",
       " 'time_used': 153,\n",
       " 'outer_id': 'secondfaceset'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/delete\"\n",
    "\n",
    "# 3.权限/通行证————所有 API Key 都可以调用本 API。\n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"outer_id\":\"secondfaceset\",\n",
    "    \"check_empty\":1\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d0c3ae5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 97,\n",
       " 'facesets': [{'faceset_token': '5080109ae9e463c1f851d3bbf4aa655b',\n",
       "   'outer_id': 'firstfaceset',\n",
       "   'display_name': 'news_1',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647423250,ea08c845-58b3-4c6f-9a9d-5246fd8bfdd1'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查现在API Key 中的FaceSet\n",
    "face_detect = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()\n",
    "\n",
    "# 只有1个faceset_token 说明删除成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fca72a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d1a18709",
   "metadata": {},
   "source": [
    "## [人脸搜索API  ](https://console.faceplusplus.com.cn/documents/4888381)\n",
    "* 在一个已有的 FaceSet 中找出与目标人脸最相似的一张或多张人脸，返回置信度和不同误识率下的阈值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "42fe3443",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'image_id': '6oxCPJQ6stkskCTOscON0g==',\n",
       " 'faces': [{'face_rectangle': {'width': 137,\n",
       "    'top': 57,\n",
       "    'left': 126,\n",
       "    'height': 137},\n",
       "   'face_token': '36f09ad149e3065f7242ede1137791f2'}],\n",
       " 'time_used': 848,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'request_id': '1647424274,9ac94e8a-6acf-4cdb-8160-4e7487a5dd4b',\n",
       " 'results': [{'confidence': 79.294,\n",
       "   'user_id': '',\n",
       "   'face_token': 'befd5bf20ea0047c2c565fae0dd417ee'}]}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.导入\n",
    "import requests\n",
    "\n",
    "# 2.url  \n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "\n",
    "# 3.通行证\n",
    "API_key = \"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\"\n",
    "API_sercret = \"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\"  \n",
    "\n",
    "# 4.调用方法POST\n",
    "\n",
    "# 5.传递参数\n",
    "payload = {\n",
    "    \"api_key\":\"biwZehUGhGp1ZwtyL0OaDDtSsod-tqIz\",\n",
    "    \"api_secret\":\"-KEyjyw1V66uzlHIAMuE95MfjePAk71B\",\n",
    "    \"image_url\":\"https://p0.ssl.qhimgs1.com/sdr/400__/t01bfec77b7aadfd86a.jpg\",\n",
    "    \"outer_id\":\"firstfaceset\",\n",
    "    \"return_result_count\":1\n",
    "}\n",
    "\n",
    "# 6.发起请求  \n",
    "r = requests.post(url = face_detect, params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ae86371",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
